USE Saffron
GO

--Message
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FKC1F33ACCA0B34F75]') AND parent_object_id = OBJECT_ID(N'[dbo].[Message]'))
ALTER TABLE [dbo].[Message]  WITH CHECK ADD  CONSTRAINT [FKC1F33ACCA0B34F75] FOREIGN KEY([SentBy])
REFERENCES [dbo].[UserAccount] ([Id])
GO
 
--Message Info 
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK60097BFA6FF387F0]') AND parent_object_id = OBJECT_ID(N'[dbo].[MessageInfo]'))
ALTER TABLE [dbo].[MessageInfo]  WITH CHECK ADD  CONSTRAINT [FK60097BFA6FF387F0] FOREIGN KEY([SentMessage])
REFERENCES [dbo].[Message] ([Id])
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK60097BFAA0C94F8B]') AND parent_object_id = OBJECT_ID(N'[dbo].[MessageInfo]'))
ALTER TABLE [dbo].[MessageInfo]  WITH CHECK ADD  CONSTRAINT [FK60097BFAA0C94F8B] FOREIGN KEY([SentTo])
REFERENCES [dbo].[UserAccount] ([Id])
GO

--Project
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FKCFC6D85AF661AA11]') AND parent_object_id = OBJECT_ID(N'[dbo].[Project]'))
ALTER TABLE [dbo].[Project]  WITH CHECK ADD  CONSTRAINT [FKCFC6D85AF661AA11] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[UserAccount] ([Id])
GO

--ProjectMembers

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK7DAD617F514553A5]') AND parent_object_id = OBJECT_ID(N'[dbo].[ProjectMembers]'))
ALTER TABLE [dbo].[ProjectMembers]  WITH CHECK ADD  CONSTRAINT [FK7DAD617F514553A5] FOREIGN KEY([UserAccountId])
REFERENCES [dbo].[Project] ([Id])
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK7DAD617FAD826D84]') AND parent_object_id = OBJECT_ID(N'[dbo].[ProjectMembers]'))
ALTER TABLE [dbo].[ProjectMembers]  WITH CHECK ADD  CONSTRAINT [FK7DAD617FAD826D84] FOREIGN KEY([ProjectId])
REFERENCES [dbo].[UserAccount] ([Id])
GO

--Task

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK1B248E9776061EA1]') AND parent_object_id = OBJECT_ID(N'[dbo].[Task]'))
ALTER TABLE [dbo].[Task]  WITH CHECK ADD  CONSTRAINT [FK1B248E9776061EA1] FOREIGN KEY([TaskProject])
REFERENCES [dbo].[Project] ([Id])
GO

--TaskMembers

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK17553397514553A5]') AND parent_object_id = OBJECT_ID(N'[dbo].[TaskMembers]'))
ALTER TABLE [dbo].[TaskMembers]  WITH CHECK ADD  CONSTRAINT [FK17553397514553A5] FOREIGN KEY([UserAccountId])
REFERENCES [dbo].[UserAccount] ([Id])
GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK17553397D4574CA4]') AND parent_object_id = OBJECT_ID(N'[dbo].[TaskMembers]'))
ALTER TABLE [dbo].[TaskMembers]  WITH CHECK ADD  CONSTRAINT [FK17553397D4574CA4] FOREIGN KEY([TaskId])
REFERENCES [dbo].[Task] ([Id])
GO

--TaskOwners

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK3919BED5514553A5]') AND parent_object_id = OBJECT_ID(N'[dbo].[TaskOwners]'))
ALTER TABLE [dbo].[TaskOwners]  WITH CHECK ADD  CONSTRAINT [FK3919BED5514553A5] FOREIGN KEY([UserAccountId])
REFERENCES [dbo].[UserAccount] ([Id])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK3919BED5D4574CA4]') AND parent_object_id = OBJECT_ID(N'[dbo].[TaskOwners]'))
ALTER TABLE [dbo].[TaskOwners]  WITH CHECK ADD  CONSTRAINT [FK3919BED5D4574CA4] FOREIGN KEY([TaskId])
REFERENCES [dbo].[Task] ([Id])
GO

--UserAccount

--UserAccountAttribute
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK319FA1033FB18842]') AND parent_object_id = OBJECT_ID(N'[dbo].[UserAccountAttribute]'))
ALTER TABLE [dbo].[UserAccountAttribute]  WITH CHECK ADD  CONSTRAINT [FK319FA1033FB18842] FOREIGN KEY([UserAttributeId])
REFERENCES [dbo].[UserAttribute] ([Id])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK319FA103514553A5]') AND parent_object_id = OBJECT_ID(N'[dbo].[UserAccountAttribute]'))
ALTER TABLE [dbo].[UserAccountAttribute]  WITH CHECK ADD  CONSTRAINT [FK319FA103514553A5] FOREIGN KEY([UserAccountId])
REFERENCES [dbo].[UserAccount] ([Id])
GO

--UserAccountSystemRole
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FKA0C9AF52514553A5]') AND parent_object_id = OBJECT_ID(N'[dbo].[UserAccountSystemRole]'))
ALTER TABLE [dbo].[UserAccountSystemRole]  WITH CHECK ADD  CONSTRAINT [FKA0C9AF52514553A5] FOREIGN KEY([UserAccountId])
REFERENCES [dbo].[UserAccount] ([Id])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FKA0C9AF5255B9C55C]') AND parent_object_id = OBJECT_ID(N'[dbo].[UserAccountSystemRole]'))
ALTER TABLE [dbo].[UserAccountSystemRole]  WITH CHECK ADD  CONSTRAINT [FKA0C9AF5255B9C55C] FOREIGN KEY([SystemRoleId])
REFERENCES [dbo].[SystemRole] ([Id])
GO
--SystemRole

--UserAttribute
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK34C9ED74B7AB4102]') AND parent_object_id = OBJECT_ID(N'[dbo].[UserAttribute]'))
ALTER TABLE [dbo].[UserAttribute]  WITH CHECK ADD  CONSTRAINT [FK34C9ED74B7AB4102] FOREIGN KEY([UserAttributeGroupId])
REFERENCES [dbo].[UserAttributeGroup] ([Id])
GO

--UserAttributeGroup

